// import Vue from 'vue'

const filters = function (Vue) {
  
    //截取字体长度
    Vue.filter("ellipsis", function (value, num) {
        if (!value) return "";
        if (value.length > num) {
          return value.slice(0, num) + "...";
        }
        return value;
      });
      Vue.filter("setTime", function (date) {
        if (date) {
          var dateOld = Number(date);
          // 当前时间
          let nYear = new Date().getFullYear();
          let nMonth = new Date().getMonth() + 1;
          let nDay = new Date().getDate();
          // 传递过来的年月日
          let time = new Date(dateOld);
          let year = time.getFullYear();
          let month = time.getMonth() + 1;
          let hours = time.getHours();
          let minutes = time.getMinutes();
          let day = time.getDate();
          if (year < nYear) {
            return year + "年" + month + "月" + day + "日";
          } else if (month != nMonth || day != nDay) {
            return month + "月" + day + "日";
          } else {
            if (minutes < 10) {
              minutes = "0" + minutes;
            }
            return hours + ":" + minutes;
          }
        } else {
          return;
        }
      });
      Vue.filter("broadTime", function (date) {
        if (date) {
          var dateOld = Number(date);
          // 当前时间
          let nYear = new Date().getFullYear();
          let nMonth = new Date().getMonth() + 1;
          let nDay = new Date().getDate();
          // 传递过来的年月日
          let time = new Date(dateOld);
          let year = time.getFullYear();
          let month = time.getMonth() + 1;
          let hours = time.getHours();
          let minutes = time.getMinutes();
          let day = time.getDate();
          if (year < nYear) {
            return (
              year + "年" + month + "月" + day + "日" + "  " + hours + ":" + minutes
            );
          } else {
            if (minutes < 10) {
              minutes = "0" + minutes;
            }
            return month + "月" + day + "日" + "  " + hours + ":" + minutes;
          }
        } else {
          return;
        }
      });
      Vue.filter("timeFormat", function (date) {
        if (date) {
          var dateOld = Number(date);
          // 当前时间
          let nYear = new Date().getFullYear();
          let nMonth = new Date().getMonth() + 1;
          let nDay = new Date().getDate();
          // 传递过来的年月日
          let time = new Date(dateOld);
          let year = time.getFullYear();
          let month = time.getMonth() + 1;
          let hours = time.getHours();
          let minutes = time.getMinutes();
          let day = time.getDate();
          if (year < nYear) {
            if (minutes < 10) {
              minutes = "0" + minutes;
            }
            return (
              year + "年" + month + "月" + day + "日" + "  " + hours + ":" + minutes
            );
          } else if (month != nMonth || day != nDay) {
            if (minutes < 10) {
              minutes = "0" + minutes;
            }
            return month + "月" + day + "日" + "  " + hours + ":" + minutes;
          } else {
            if (minutes < 10) {
              minutes = "0" + minutes;
            }
            return hours + ":" + minutes;
          }
        } else {
          return;
        }
      });
      // 当前时间 年月日时分秒
      Vue.filter("getTimeAll", function (date) {
        let time = new Date(parseInt(date));
        let year = time.getFullYear()
        let month = time.getMonth() + 1 >= 10 ? time.getMonth() + 1 : "0" + (time.getMonth() + 1);
        let day = time.getDate() >= 10 ? time.getDate() : "0" + time.getDate();
        let hours = time.getHours() >= 10 ? time.getHours() : '0' + time.getHours();
        let minutes = time.getMinutes() >= 10 ? time.getMinutes() : '0' + time.getMinutes();
        let seconds = time.getSeconds() >= 10 ? time.getSeconds() : '0' + time.getSeconds();
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
      })

       //文件大小显示
  Vue.filter("convertByte", function (byteCount) {
    var count = parseFloat(byteCount || 0),
      unitArr = ['b', 'Kb', 'Mb', 'Gb', 'Tb'],
      index = 0;
    if (!count) return 0;
    for (let i = 0; i < 5; i++) {
      if (count >= 1024) {
        count = count / 1024
        index++
      } else {
        return index > 0 ? count.toFixed(2) + unitArr[index] : count + unitArr[index]
      }
      if (index == 4) {
        return count.toFixed(2) + unitArr[index]
      }
    }
  })
}

export default filters  